package com.czh.service.impl;

import com.czh.convert.UserConvert;
import com.czh.dao.UserDao;
import com.czh.dto.UserQueryDTO;
import com.czh.entity.User;
import com.czh.service.UserService;
import com.czh.utils.PageInfo;
import com.czh.utils.PageUtils;
import com.czh.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (User)服务接口实现层
 *
 * @author makejava
 * @since 2024-07-24 17:00:54
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource(type = UserDao.class)
    private UserDao userDao;


    @Override
    public List<User> pageList(Integer page, Integer size) {
        Integer start = (page - 1) * size;
        return userDao.pageList(start, size);
    }

    /**
     * 查询列表
     *
     * @param user 对象
     * @return
     */
    @Override
    public List<User> list(User user) {
        return userDao.findList(user);
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return
     */
    @Override
    public User detail(Long id) {
        return userDao.findById(id);
    }

    /**
     * 添加
     *
     * @param user 对象
     * @return
     */
    @Override
    public int save(User user) {
        return userDao.insertSelective(user);
    }

    /**
     * 修改
     *
     * @param user 对象 根据主键修改
     * @return
     */
    @Override
    public int edit(User user) {
        return userDao.updateSelective(user);
    }

    /**
     * 删除
     *
     * @param id 主键
     * @return
     */
    @Override
    public int delete(Long id) {
        return userDao.deleteById(id);
    }

    /**
     * 批量删除
     *
     * @param ids 主键集合
     * @return
     */
    @Override
    public int deleteByIds(List<Long> ids) {
        return userDao.deleteByIds(ids);
    }

    @Override
    public List<User> pageBySimple(int page, int size) {
        page = (page - 1) * size;
        List<User> list = userDao.pageBySimple(page, size);
        return list;
    }

    @Override
    public PageUtils pageByCustom(UserQueryDTO userQueryDTO) {
        Integer pageNumber = userQueryDTO.getPage();
        Integer pageSize = userQueryDTO.getSize();

        PageUtils page = new PageUtils();
        Integer pageBegin = (pageNumber - 1) * pageSize;
        //当前页码
        page.setPageNum(pageNumber);
        //前（后）一页页码
        page.setPrePage(pageNumber - 1);
        page.setNextPage(pageNumber + 1);
        //开始（末尾）数据是第几行
        page.setStartRow(pageBegin + 1);
        page.setEndRow(pageBegin + pageSize + 1);
        //每页数据条数
        page.setPageSize(pageSize);
        //总页数
        Integer div = page.getSize() / pageSize;
        Integer pages = page.getSize() % pageSize == 0 ? div : div + 1;
        page.setPages(pages);
        //是否是第（最后）一页,是否有前（后）一页
        page.setIsFirstPage(pageNumber == 1);
        page.setHasPreviousPage(pageNumber != 1);
        page.setIsLastPage(pageNumber.equals(pages));
        page.setHasNextPage(!pageNumber.equals(pages));

        //总数据条数
        page.setSize(userDao.selectSize(userQueryDTO));
        //查询到的数据
        userQueryDTO.setPage(pageBegin);
        List<User> list = userDao.pageByCustom(userQueryDTO);
        // 类型转换以及将id转成名称
        List<UserVO> collect = list.stream().map(user -> {
            UserVO userVO = UserConvert.INSTANCE.do2vo(user);
            userVO.setIdName(getIdName(userVO.getId()));
            return userVO;
        }).collect(Collectors.toList());
        page.setTableData(collect);
        return page;
    }

    @Override
    public PageInfo pageByCustom2(UserQueryDTO userQueryDTO) {
        // 获取分页的开始的数据
        UserQueryDTO queryDTO = new UserQueryDTO();
        BeanUtils.copyProperties(userQueryDTO, queryDTO);
        queryDTO.setPage((userQueryDTO.getPage() - 1) * userQueryDTO.getSize());
        // 根据条件进行查询
        List<User> list = userDao.pageByCustom(queryDTO);
        PageInfo<UserVO> result = new PageInfo<>();
        // 类型转换以及将id转成名称
        List<UserVO> collect = list.stream().map(user -> {
            UserVO userVO = UserConvert.INSTANCE.do2vo(user);
            userVO.setIdName(getIdName(userVO.getId()));
            return userVO;
        }).collect(Collectors.toList());
        // 获取了总数
        int totalItems = userDao.selectSize(queryDTO);
        result.setData(collect);
        result.setPageSize(userQueryDTO.getSize());
        result.setCurrentPage(userQueryDTO.getPage());
        result.setTotalItems(totalItems);
        result.setTotalPages((int) Math.ceil((double) totalItems / userQueryDTO.getSize()));
        return result;
    }

    private String getIdName(Long id) {
        if (id < 10) {
            return "小于10";
        } else {
            return "大于10";
        }
    }
}

